/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted


/// Generating total Protestant respondants by year

by year: egen Protestantresp = count(religion) if religion==1 

/// Generating total number of Protestant voters by year

by year: egen Protestantvoted = count(voted) if religion==1 & voted==3 & race==1

/// Generating total number of Protestant non-voters by year

by year: egen ProtestantNvoted = count(voted) if voted==0 & religion==1 & race==1 | voted==1 & religion==1 & race==1 | voted==2 & religion==1 & race==1


/// Generating total number of Non-Protestant voters by year

by year: egen NProtestantvoted = count(voted) if religion~=1 & voted==3 | religion ==1 & race~=1

/// Generating total number of Non-Protestant non-voters by year

by year: egen NProtestantNvoted = count(voted) if religion~=1 & voted==0 | religion~=1 & voted==1 | religion~=1 & voted==2 | religion==1 & voted==0 & race~=1 | religion==1 & voted==1 & race~=1 | religion==1 & voted==2 & race~=1

/// filling in the blanks

by year: egen protestantvoted = max(Protestantvoted)
by year: egen protestantNvoted = max(ProtestantNvoted)
by year: egen Nprotestantvoted = max(NProtestantvoted)
by year: egen NprotestantNvoted = max(NProtestantNvoted)




/// Generating % religion TO with Mosteller Correction

gen N = 1
gen a = [protestantvoted * NprotestantNvoted] / [protestantNvoted * Nprotestantvoted]


///Generating filler vars

gen aminone =(a-1)
gen AprotestantplusTO = ((AProtestant-ABlack)+aTO)
gen aprotestantTO=(a*(AProtestant-ABlack)*aTO)

gen CprotestantTO = 1/ (2*(aminone)) * (N+(aminone*AprotestantplusTO) - sqrt((N+aminone*AprotestantplusTO)*(N+aminone*AprotestantplusTO) - (4*(aminone*aprotestantTO))))

///Adjusting TO for the formula

replace CprotestantTO = CprotestantTO/(AProtestant - ABlack)



/// Moving to Partisanship - Generating Protestant Republican Vote total by year

by year: egen protestantrep = count(religion) if presvote==2 & religion==1 & race==1

/// Generating Protestant Non-Republican Vote by Year

by year: egen protestantNrep = count(religion) if presvote==1 & religion==1 & race==1

// Generating non-Protestant Republican Vote total by year

by year: egen Nprotestantrep = count(religion) if presvote==2 & religion~=1 | presvote==2 & religion==1 & race~=1


/// Generating Non-Protestant Non-Republican vote total by year

by year: egen NprotestantNrep = count(religion) if presvote==1 & religion~=1 | presvote==1 & religion==1 & race~=1

/// filling in the blanks

by year: egen ProtestantRep = max(protestantrep)
by year: egen ProtestantNRep = max(protestantNrep)
by year: egen NProtestantRep = max(Nprotestantrep)
by year: egen NProtestantNRep = max(NprotestantNrep)

/// Generation % religion Repocrat with Mosteller Correction

gen A = (ProtestantRep*NProtestantNRep)/(ProtestantNRep*NProtestantRep)
gen Aminone =(A-1)
gen AprotestantplusRep = ((AProtestant-ABlack)+(ARep))
gen AprotestantRep=(A*(AProtestant-ABlack)*(ARep))
gen CprotestantRep = 1/(2*(Aminone)) * (N+(Aminone*AprotestantplusRep) - sqrt((N+Aminone*AprotestantplusRep)*(N+Aminone*AprotestantplusRep) - (4*(Aminone*AprotestantRep))))

replace CprotestantRep = CprotestantRep/(AProtestant - ABlack)


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen protestantcoalcontrib = (CprotestantTO* CprotestantRep *(AProtestant-ABlack))/(aTO*ARep)



